﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Common;
using System.Web.UI.WebControls;
using VideoClassLibrary.Model;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Reflection;
using System.Text.RegularExpressions;

namespace ELearningBusinessHelper.CommonFun
{
    /// <summary>
    /// 系统栏目操作
    /// </summary>
    public class SystemFunction
    {
        #region IDataReader转换成实体

        /// <summary>
        /// 从DataReader里读取数据将数据转换成实例列表
        /// </summary>
        /// <typeparam name="T">实例类型</typeparam>
        /// <param name="reader">DataReader</param>
        /// <returns>列表</returns>
        public static IList<T> GetListFromReader<T>(IDataReader reader)
        {
            IList<T> list = new List<T>();
            Type type = typeof(T);
            PropertyInfo[] props = type.GetProperties();
            DataTable table = null;
            List<string> namesInSelect = new List<string>();
            while (reader.Read())
            {
                if (table == null)
                {
                    table = reader.GetSchemaTable();
                    foreach (DataRow row in table.Rows)
                    {
                        namesInSelect.Add(row[0].ToString().ToLower());
                    }
                }
                T instance = Activator.CreateInstance<T>();
                foreach (PropertyInfo property in props)
                {
                    if (property.PropertyType.FullName.StartsWith("System.Collections.Generic.IList`1"))
                    {
                        continue;
                    }
                    if (!namesInSelect.Contains(property.Name.ToLower()))
                    {
                        continue;
                    }
                    Object value = reader[property.Name];
                    if (Convert.IsDBNull(value))
                    {
                        if (value is string)
                        {
                            property.SetValue(instance, String.Empty, new object[] { });
                        }
                    }
                    else
                    {
                        property.SetValue(instance, value, new object[] { });
                    }
                }
                list.Add(instance);
            }
            return list;
        }

        /// <summary>
        /// 从DataReader里读取数据将数据转换成实例
        /// </summary>
        /// <typeparam name="T">实体类型</typeparam>
        /// <param name="reader">DataReader</param>
        /// <returns>实例</returns>
        public static T GetEntityFromReader<T>(IDataReader reader)
        {
            Type type = typeof(T);
            PropertyInfo[] props = type.GetProperties();
            T instance = Activator.CreateInstance<T>();
            DataTable table = null;
            List<string> namesInSelect = new List<string>();
            if (reader.Read())
            {
                if (table == null)
                {
                    table = reader.GetSchemaTable();
                    foreach (DataRow row in table.Rows)
                    {
                        namesInSelect.Add(row[0].ToString().ToLower());
                    }
                }
                foreach (PropertyInfo property in props)
                {
                    if (property.PropertyType.FullName.StartsWith("System.Collections.Generic.IList`1"))
                    {
                        continue;
                    }
                    if (!namesInSelect.Contains(property.Name.ToLower()))
                    {
                        continue;
                    }
                    Object value = reader[property.Name];
                    if (Convert.IsDBNull(value))
                    {
                        if (value is string)
                        {
                            property.SetValue(instance, String.Empty, new object[] { });
                        }
                    }
                    else
                    {
                        property.SetValue(instance, value, new object[] { });
                    }
                }
            }
            else
            {
                return default(T);
            }
            return instance;
        }

        #endregion 
    }
}